import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    //模拟实现一个Tcp协议的回显客户端；
    private Socket socket1=null;
    public TcpEchoClient(String serverIP,int serverPort) throws IOException {
        socket1=new Socket(serverIP,serverPort);
    }
    //进行启动客户端；
    public void start()  {
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream=socket1.getInputStream();
            OutputStream outputStream=socket1.getOutputStream()) {
            Scanner scanner1 = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while (true) {
                System.out.println("请输入你想要发送的请求");
                String request = scanner.next();
                printWriter.println(request);
                printWriter.flush();
                String response=scanner1.next();
                System.out.println(response);
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1", 9090);
        tcpEchoClient.start();
    }
}
